<?php

namespace app\model;

use app\service\RedisService;
use app\stateData;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;

class OrderEnterpriseCompany extends Model
{


    /**
     * 公司证书表
     */
    use SoftDelete;
    protected $createTime='create_date';
    protected $deleteTime = 'delete_date';
    public function getStatusAttr($name)
    {
        if (empty($name)) return '';

        return [
            'id'=>$name,
            'text'=>stateData::$_enter_certificate_reg[$name]
        ];
    }
    public function addDataModel($data){
        return self::create($data);
    }

    public function certificates()
    {
        return $this->hasOne (OrderEnterpriseCertificateRegister::class,'certificate_code','enter_certificate_code')
//        return $this->hasOne (OrderEnterpriseCertificateRegister::class,'certificate_code','certificate_code')
            ->bind ([
                'sale_org',
                'sale_uid',
                'reg_unit',
                'price_unit',
                'certificate_leixing',
                'certificate_leibie',
                'certificate_dengji',
                'certificate_zhuanye',
                'certificate_b',
                'social_security',
                'cycle',
                'project',
                'people_num',
                'business_id',
                'type',
                'certificate_id'
            ])->removeOption('delete_date');
    }
    public function chargeback()
    {
        return $this->hasOne (OrderDealChargeback::class,'certificate_code','certificate_code')
            ->bind ([
                'order_company_id',
                'chargeback_performance'=>'performance',
                'charge_back_date',
                'reason',
                'get_uid_up_logoff_file',
                'notifier_get_uid',
                'push_uidup_logoff_file',
                'notifier_push_uid',
                'b_certificate_is_conpany',
                'chargeback_create_date'=>'create_date',
                'chargeback_type'=>'type'
            ])->order('create_date DESC');
    }

    public function priceUnit()
    {
        return $this->hasOne (OrderEnterpriseCertificateRegister::class,'certificate_code','enter_certificate_code')->bind (['price_unit','people_num','certificate_id','certificate_leixing']);
//        return $this->hasOne (OrderEnterpriseCertificateRegister::class,'certificate_code','certificate_code')->bind (['price_unit','people_num','certificate_id','certificate_leixing']);
    }

    public function businesss()
    {
        return $this->hasOne (OrderEnterpriseCertificateRegister::class,'certificate_code','enter_certificate_code')->bind (['business_id']);
//        return $this->hasOne (OrderEnterpriseCertificateRegister::class,'certificate_code','certificate_code')->bind (['business_id']);
    }
    public function getGetUidAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>(new User())->getIdNickName($name)?:''
        ];
    }
    public function getGetOrgAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>(new UserOrganization())->getStructureName($name)?:''
        ];
    }

    public function getOutUidAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>(new User())->getIdNickName($name)?:''
        ];
    }
    public function getOutOrgAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>(new UserOrganization())->getStructureName($name)?:''
        ];
    }

    public function getSfkStatusAttr($name)
    {
        if (!$name){
            return [
                'id'=>'',
                'text'=>''
            ];
        }
        return [
            'id'=>$name?:'',
            'text'=>stateData::$_sfk_status[$name]
        ];
    }
    public function getInfoFileNameAttr($name)
    {
        return $name?:'';
    }
    public function getRegFromFileAttr($value)
    {
        if (empty($value)) return "";
        $arr = explode(',',$value);
        $value = '';
        foreach($arr as &$v){
            $value .= config('file.url').$v .',';
        }
        return rtrim($value,',');
    }

    public function getPersonInformationFileAttr($value)
    {
        if (empty($value)) return "";
        $arr = explode(',',$value);
        $value = '';
        foreach($arr as &$v){
            $value .= config('file.url').$v .',';
        }
        return rtrim($value,',');
    }

//    public function getContractPriceAttr($name)
//    {
//        if ($name == '0.00'){
//            return '';
//        }
//        return $name;
//    }
    public function getSaleDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00' || empty($name)){
            return "";
        }
//        return date ('Y-m-d',strtotime ($name));
        return $name;
    }
    public function getSfkWcDateAttr($name)
    {
        if (!$name || $name =='0000-00-00 00:00:00') return '';
        return date ('Y-m-d',strtotime ($name));
    }

    public function getCertificateIdAttr($name): array
    {
        if (empty($name)) return [];
        return array_map ('intval', explode (',', $name));
    }

    public function getProjectAttr($value): array
    {
        if ($value === '') {
            return [
                'id' => '',
                'text' => ''
            ];
        }
        return [
            'id' => (int)$value,
            'text' => stateData::$_project[$value]
        ];
    }

    public function getCertificateBAttr($value): array
    {
        if ($value === '') {
            return [
                'id' => '',
                'text' => ''
            ];
        }
        return [
            'id' => (int)$value,
            'text' => stateData::$_b_doc[$value]
        ];
    }

    public function getSocialSecurityAttr($value): array
    {
        if ($value === '') {
            return [
                'id' => '',
                'text' => ''
            ];
        }

        return [
            'id' => (int)$value,
            'text' => stateData::$_social_security[$value]
        ];
    }

    public function getCycleAttr($value): array
    {
        if ($value === '' || $value == 0) {
            return [
                'id' => '',
                'text' => ''
            ];
        }
        return [
            'id' => (int)$value,
            'text' => stateData::$_cycle[$value]
        ];

    }

    public function getTransferAttr($value): array
    {
        $values = $value ?: '';
        return [
            'id' => $values,
            'text' => stateData::$_transfer[$value]
        ];
    }
    public function getBusinessIdAttr($name)
    {
        return [
            'id' => $name,
            'text' => (new SystemBusiness)->getBusinessIdTitle ($name) ?: ''
        ];

    }
    public function getNoticeIdAttr($name): array
    {
        return [
            'id' => $name,
            'text' => (new User)->getIdNickName ($name) ?: ''
        ];
    }

    public function getSaleOrgAttr($name): array
    {
        return [
            'id' => $name,
            'text' => (new UserOrganization)->getStructureName ($name) ?: ''
        ];
    }

    public function getSaleUidAttr($name): array
    {
        return [
            'id' => $name ?: '',
            'text' => (new User)->getIdNickName ($name) ?: ''
        ];
    }

    //公示电子证书文件
    public function getElectronicCertificateFileAttr($value)
    {
        if (!empty($value)) {
            $value = str_replace ('\\', '/', 'http://' . $_SERVER['HTTP_HOST'] . '/storage/' . $value);
        }
        return $value;
    }

    //B证电子证书文件
    public function getBElectronicCertificateFileAttr($value)
    {
        if (!empty($value)) {
            $value = str_replace ('\\', '/', 'http://' . $_SERVER['HTTP_HOST'] . '/storage/' . $value);
        }

        return $value;
    }

    public function getBRegElectronicCertificateDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00') return "";
        return $name;
    }

    public function getBElectronicCertificateDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00') return "";
        return $name;
    }

    public function getElectronicCertificateDateAttr($name)
    {
        if ($name === '0000-00-00 00:00:00') return "";
        return $name;
    }

    public function getCertificateLeixingAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel ($name) ?: '';
//        return [
//            'id'=>$name,
//            'text'=>(new SystemCertificate)->getWhereIdCertificateModel ($name)?:''
//        ];
    }

    public function getCertificateLeibieAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel ($name) ?: '';

    }

    public function getCertificateDengjiAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel ($name) ?: '';
//        return [
//            'id'=>$name,
//            'text'=>(new SystemCertificate)->getWhereIdCertificateModel ($name)?:''
//        ];
    }

    public function getCertificateZhuanyeAttr($name)
    {
        if ($name == 0) {
            return "";
        }
        return (new SystemCertificate)->getWhereIdCertificateModel ($name) ?: '';
//        return [
//            'id'=>$name,
//            'text'=>(new SystemCertificate)->getWhereIdCertificateModel ($name)?:''
//        ];
    }


    /**
     * 查询补充证书id是否存在
     * @return array|null
     */
    public function getCertificateCodeFindModel($cer)
    {
//        $data =  (new OrderEnterpriseCompany)->withJoin('certificates')
        $data =  $this->where (['certificate_code'=>$cer])
            ->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    public function getIdCertificateCodeFindModel($id,$certificate_code): ?array
    {
        $data = $this->where (['certificate_code'=>$certificate_code,'id'=>$id])->findOrEmpty ();
        if (!$data ->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 获取用户权限范围内的数据
     */
    public function getUserRandDataModel($idArr)
    {
        if (is_array($idArr)) {
            $idStr = implode(',',$idArr);
        }
        return $this->whereRaw('responsibility_uid in ('.$idStr.') OR get_uid in ('.$idStr.') OR sale_uid  in ('.$idStr.')')->column ('id,certificate_code,sale_date');

        // return $this->whereIn ('responsibility_uid',$idArr)->whereIn('get_uid',$idArr)->whereIn ('sale_uid',$idArr)->column ('id,certificate_code,sale_date');
    }

    /**
     * 获取订单业绩归属人
     */
    public function getOrderPerformanceUidModel($id,$certificateCode)
    {
        return $this->where (['certificate_code'=>$certificateCode,'id'=>$id])->value ('performance_uid');
    }

    /**
     * 通过id编辑第三步资料
     * @param $id
     * @param $data
     * @return \app\model\OrderEnterpriseCompany
     */
    public function editUnitInfoDataModel($data)
    {
        return $this->where (['certificate_code'=>$data['certificate_code'],'id'=>$data['id']])->update ($data);
    }

    public function getResponsibilityUidModel($id)
    {
//        return $this->with ('certificates')->where (['responsibility_uid'=>$id])->where('get_uid','<>',$id)->select();
        return $this->where (['responsibility_uid'=>$id])->where('get_uid','<>',$id)->select();
    }

    public function person()
    {
        return $this->hasOne (OrderPersonCertificateRegister::class,'person_id_card','person_id_card');
    }

    /**
     * 添加收付款查询所有补充资料库完成的数据
     * @param $cer
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCertificateCodeDataModel($cer,$person_name,$employ_unit_title)
    {
//        if (empty($person_name)){
//            $data = OrderEnterpriseCompany::with('certificates')->whereOr (['employ_unit_title'=>$employ_unit_title])->where (['order_enterprise_company.certificate_code'=>$cer,'sfk_status'=>1])->select()->toArray ();
//        }else if (empty($employ_unit_title)){
//            $data = OrderEnterpriseCompany::with('certificates')->whereOr (['person_name'=>$person_name])->where (['order_enterprise_company.certificate_code'=>$cer,'sfk_status'=>1])->select()->toArray ();
//        }else{
//        $data = OrderEnterpriseCompany::with('certificates')->where (['employ_unit_title'=>$employ_unit_title])->where (['sfk_status'=>1])->select()->toArray ();
        //        }
        return $this->where (['employ_unit_title'=>$employ_unit_title])->where (['sfk_status'=>1])->select()->toArray ();
    }

    public function getCertificateCodeFindDataModel($id,$cer,$where='')
    {
//        $data = OrderEnterpriseCompany::with('certificates')->where($where)->where (['order_enterprise_company.certificate_code'=>$cer,'order_enterprise_company.id'=>$id])->findOrEmpty();
        $data = $this->where($where)->where (['order_enterprise_company.certificate_code'=>$cer,'order_enterprise_company.id'=>$id])->findOrEmpty();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 获取证书应收款
     * @param $id
     * @param $cer
     * @return array|null
     */
    public function getFinanceFieldIdFindModel($id,$cer)
    {
//        $data =  (new OrderEnterpriseCompany)->withJoin('certificates')
        $data =  $this->where (['certificate_code'=>$cer])
            ->where (['id'=>$id])
            ->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }



    /**
     * 获取订单详情
     * @param $id
     * @param $cer
     * @return array|null
     */
    public function getCerIdFindModel($id,$cer){

//        $data =  (new OrderEnterpriseCompany)->withJoin('certificates')
        $data =  $this->where (['order_enterprise_company.certificate_code'=>$cer,'order_enterprise_company.id'=>$id])
            ->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    public function getCerIdFindSelectModel($id,$cer){
//        return  (new OrderEnterpriseCompany)->withJoin('certificates')
        return  $this->where (['order_enterprise_company.certificate_code'=>$cer,'order_enterprise_company.id'=>$id])
            ->select()->toArray ();
    }


    /**
     * 修改订单状态为已退单
     * @param $cer
     * @param int $status
     * @return \app\model\OrderEnterpriseCompany
     */
    public function setStatusBack($cer, int $status=3)
    {
        return $this->where (['certificate_code'=>$cer])->update (['status'=>$status]);
    }

    /**
     * 修改订单状态状态为已售出
     * @param $cer
     * @param int $status
     * @param string $date
     * @return \app\model\OrderEnterpriseCompany
     */
    public function updateStatus($cer,string $date,int $status=1)
    {
        return $this->where (['certificate_code'=>$cer])->update (['status'=>$status,'sale_date'=>$date]);
    }

    /**
     * 通过身份证号码查询附属业务
     * @param $idCard
     * @return mixed
     */
    public function getIdCardAncillary($idCard)
    {
        return $this->where (['person_id_card'=>$idCard])->value ('ancillary_businesses_id');
    }

    /**
     * 通过企业补充id查询证书编码
     * @param $id
     * @return mixed
     */
    public function getIdValueCer($id)
    {
        return $this->where (['id'=>$id])->value ('certificate_code');
    }

    /**
     * 查询证书列表
     * @param $idArr
     * @param $page
     * @param $pageNum
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCompanyListModel($idArr,$page,$pageNum,$where='')
    {
        $chargebackModel = new OrderDealChargeback();

        $data = $this->where($where)->column ('id,certificate_code,sale_uid,sale_date,get_uid,responsibility_uid,submit_id');
        //判断通知人 审批人等是否是当前用户
        $result = [];
        foreach ($data as $k => $item) {
            /**
             * 临时用来补充原需求表数据  2023.12.13日修改 一次性脚本
             */
//            if ($item['certificate_leixing'] == '' ){
//                $enData = Db::table('order_enterprise_certificate_register')->where (['id'=>$item['enter_id']])->find();
//
//                    $this->where (['id'=>$item['id']])->update ([
//                        'business_id' => $enData['business_id'],
//                        'sale_org' => $enData['sale_org'],
//                        'sale_uid' => $enData['sale_uid'],
//                        'reg_unit' => $enData['reg_unit'],
//                        'price_unit' => $enData['price_unit'],
//                        'certificate_id' => $enData['certificate_id'],
//                        'certificate_leixing' => $enData['certificate_leixing'],
//                        'certificate_leibie' => $enData['certificate_leibie'],
//                        'certificate_dengji' => $enData['certificate_dengji'],
//                        'certificate_zhuanye' => $enData['certificate_zhuanye'],
//                        'certificate_b' => $enData['certificate_b'],
//                        'social_security' => $enData['social_security'],
//                        'cycle' => $enData['cycle'],
//                        'project' => $enData['project'],
//                        'people_num' => $enData['people_num'],
//                        'type' => $enData['type'],
//                ]);
//            }

            //查询当前信息是否有退单|到期操作

            $find = $chargebackModel->getCidCerDataModel($item['id'],$item['certificate_code']);

            if ($find){
                if (in_array ($item['get_uid'],$idArr) || in_array ($item['responsibility_uid'],$idArr) || in_array ($item['submit_id'],$idArr) || in_array ($find['notifier_get_uid'],$idArr) || in_array ($find['notifier_push_uid'],$idArr)|| in_array ($item['sale_uid'],$idArr)){
                    $result[] = $item;
                }
            }else{
                if (in_array ($item['get_uid'],$idArr) || in_array ($item['responsibility_uid'],$idArr) || in_array ($item['submit_id'],$idArr) || in_array ($item['sale_uid'],$idArr)){
                    $result[] = $item;
                }
            }
        }
        return $result;
    }

    public function getCompanyCerIdFindModel($id,$cer)
    {
//        $data = $this->with ('certificates')->where(['id'=>$id,'certificate_code'=>$cer])->order('create_date DESC')->findOrEmpty();
        $data = $this->where(['id'=>$id,'certificate_code'=>$cer])->order('create_date DESC')->findOrEmpty();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return '';
    }

    public function getCompanyListUidModel($page,$pageNum,$id)
    {
//        return $this->with ('certificates')->where(['responsibility_uid'=>$uid])->whereOr ('submit_id',$uid)->whereOr ('get_uid',$uid)->order('create_date DESC')->select()->toArray ();
//        $data = $this->with ('certificates')->order('create_date DESC')->select()->toArray ();
//        $data = $this->order('create_date DESC')->select()->toArray ();
        $data = $this->column ('id,certificate_code,sale_date,get_uid,responsibility_uid,submit_id,sale_uid');

        //判断通知人 审批人等是否是当前用户
        $result = [];
        foreach ($data as $k => $item) {
            //查询当前信息是否有退单|到期操作
            $chargebackModel = new OrderDealChargeback();
            $find = $chargebackModel->getCidCerDataModel($item['id'],$item['certificate_code']);

            if ($find){
                if ($item['get_uid'] == $id || $item['responsibility_uid'] == $id   || $item['submit_id'] == $id || $find['notifier_get_uid'] == $id || $find['notifier_push_uid'] == $id|| $item['sale_uid'] == $id){
                    $result[] = $item;
                }
            }else{
                if ($item['get_uid'] == $id || $item['responsibility_uid'] == $id   || $item['submit_id'] == $id || $item['sale_uid'] == $id){
                    $result[] = $item;
                }
            }
        }
        return $result;
    }

    public function getCompanyIdListUidModel($page,$pageNum,$id,$uid = '')
    {
        return $this->where (['id'=>$id])->column('id,certificate_code,sale_date');

        //判断通知人 审批人等是否是当前用户
//        $result = [];
//        foreach ($data as $k => $item) {
//            //查询当前信息是否有退单|到期操作
//            $chargebackModel = new OrderDealChargeback();
//            $find = $chargebackModel->getCidCerDataModel($item['id'],$item['certificate_code']);
//
////            if ($find){
////                if ($item['get_uid']['id'] == $uid || $item['responsibility_uid'] == $uid   || $item['submit_id'] == $uid || $find['notifier_get_uid'] == $uid || $find['notifier_push_uid'] == $uid){
////                    $result[] = $item;
////                }
////            }else{
////                if ($item['get_uid']['id'] == $uid || $item['responsibility_uid'] == $uid   || $item['submit_id'] == $uid ){
//                    $result[] = $item;
////                }
////            }
//        }
//        return array_values ($result);
    }

    public function getIdDataModel($idArr)
    {
//        return $this->with ('certificates')->whereIn('id',$idArr)->order('create_date DESC')->select ()->toArray ();
        return $this->whereIn('id',$idArr)->order('create_date DESC')->select ()->toArray ();
    }
    /**
     * 查询身份证对应订单
     * @param $idCard
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonIdCardListsModel($idCard)
    {
//        return $this->with ('certificates')->where(['person_id_card'=>$idCard,'status'=>5])->order('create_date ASC')->select()->toArray ();
        return $this->where(['person_id_card'=>$idCard])->order('create_date ASC')->select()->toArray ();
    }

    /**
     * 查询身份证对应续签订单
     * @param $idCard
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonIdCardListModel($idCard)
    {
//        return $this->with ('certificates')->where(['person_id_card'=>$idCard,'status'=>5])->order('create_date ASC')->select()->toArray ();
        return $this->where(['person_id_card'=>$idCard,'status'=>5])->order('create_date ASC')->select()->toArray ();
    }



    public function getPersonIdCardFindModel($idCard)
    {
//        return $this->with ('certificates')->where(['person_id_card'=>$idCard])->order('create_date ASC')->select()->toArray ();
        return $this->where(['person_id_card'=>$idCard])->order('create_date ASC')->select()->toArray ();
    }
    public function getPersonIdCardNewOrderModel($idcard){
        return $this->where (['person_id_card'=>$idcard])->order('create_date DESC')->select ();
    }

    /**
     * 查询企业名称对应订单
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getEnterpriseName($name)
    {
        return $this->where (['employ_unit_title'=>$name])->select ()->toArray ();
    }
    public function getCompanyCountModel($where='')
    {
        return $this->where($where)->count();
    }

    /**
     * 通过id查询证书详情
     * @param $id
     * @return array|null
     */
    public function getDetailsModel($id)
    {
//        $data = $this->with ('certificates')->where (['id'=>$id])->findOrEmpty ();
        $data = $this->where (['id'=>$id])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }



    /**
     * 删除订单
     * @param $id
     * @return bool
     */
    public function delIdData($id)
    {
        return self::destroy($id);
    }

    /**
     * 更新数据
     * @param $param
     * @return \app\model\OrderEnterpriseCompany
     */
    public function editCompanyModel($param)
    {
        /**
         * 修改数据也同步更新缓存 保证业绩统计完整性
         */
        //先通过ID查询sale_date
        $saleDate = $this->where (['id'=>$param['id']])->value ('sale_date');
        if ($saleDate){
            $redisSer = new RedisService();
            $redisSer->updateDateBetweenData ($saleDate,$saleDate);
        }
        return self::update ($param);
    }

    /**
     * 证书注销操作
     * @param $id
     * @param int $status
     * @return \app\model\OrderEnterpriseCompany
     */
    public function setLogOffModel($id,$cer, int $status=4)
    {
        return $this->where (['certificate_code'=>$cer,'id'=>$id])->update (['status'=>$status]);
    }

    /**
     * 续签
     * @param $id
     * @param $cer
     * @param int $status
     * @return \app\model\OrderEnterpriseCompany
     */
    public function setXuQianModel($id,$cer, int $status=5)
    {
        return $this->where (['certificate_code'=>$cer,'id'=>$id])->update (['status'=>$status]);
    }

    public function getSummary($startDate,$endDate,$uidArr,$cerId='')
    {
        $orderCerRegModel = new OrderEnterpriseCertificateRegister();
        //查询时间段内成交库的certificate_code   order_company_id
        $data = $this->whereBetweenTime ('sale_date',$startDate,$endDate)
//            ->fetchSql(true)
            ->whereRaw('status=1 OR status=2 OR status=3 OR status=4 OR status=5')
            ->field('id,enter_certificate_code,certificate_code,performance,performance_uid,person_name,certificate_leixing')
            ->select ();

        //判断业绩归属人是否存在uidArr
        if (!is_array ($uidArr)){
            $uidArr = explode (',',$uidArr);
        }

        $performance_all = 0;
        $leixing = '';
        foreach ($data as $k => &$datum) {
            $yData = $datum->getData();
            if (!empty($cerId)){
//                $leixing = $orderCerRegModel->getCerLeixingModel($datum['enter_certificate_code']);
                if($cerId != $yData['certificate_leixing']){
                    continue;
                }
            }

//            $datum['certificate_leixing'] = $leixing;
            $datum['performance_all'] = 0;
            if (empty($datum['performance_uid'])){
                continue;
            }
            $datumPidArr = explode (',',$datum['performance_uid']);
            $datumPidCount = count($datumPidArr);
            foreach ($datumPidArr as $item) {
                if (in_array ($item, $uidArr)){
                    $performance_all += $datum['performance'] / $datumPidCount;
                }
            }
        }
        return $performance_all;
    }

    /**
     *二类业务占比
     * @param $startDate
     * @param $endDate
     * @param $uidArr
     * @param $cerId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getErleiZhanBiSummary($startDate,$endDate,$uidArr,$cerId=''): array
    {
//        $data = $this->with ('priceUnit')->whereRaw('status=1 OR status=2 OR status=4 OR status=5')->select ()->toArray ();
        $data = $this->whereRaw('status=1 OR status=2 OR status=4 OR status=5')->select ()->toArray ();
        //判断业绩归属人是否存在uidArr
        if (!is_array ($uidArr)){
            $uidArr = explode (',',$uidArr);
        }

        $arr = [];
        foreach ($data as $k => &$datum) {
            $datum['performance_all'] = 0;
            //判断时间
            if (strtotime ($startDate) >= strtotime ($datum['create_date']) || strtotime ($endDate) <= strtotime ($datum['create_date'])){
                unset($data[$k]);
                continue;
            }

            if (empty($datum['performance_uid'])){
                continue;
            }

            $datumPidArr = explode (',',$datum['performance_uid']);

            $datumPidCount = count($datumPidArr);
            foreach ($datumPidArr as $item) {

                if (in_array ($item, $uidArr)){

                    $datum['performance_all'] += $datum['performance'] / $datumPidCount;
                }
            }
        }
        return array_values ($data);
    }

    /**
     * 通过证书id查询订单关联信息
     * @param $cer
     * @return array|null
     */
    public function getOrderInfo($cer)
    {
        $data = $this->with ('person')->where (['certificate_code'=>$cer])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 成交总数量
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getOrderNumModel($startDate,$endDate,$uidArr): int
    {
        //查询成交时间
//        $dealModel = new OrderDeal();
        $count = 0;
        $data =  $this->whereBetweenTime ('sale_date',$startDate,$endDate)->whereRaw ('status = 1 OR status = 2 OR status=4 OR status =5')->select()->toArray ();

        //判断业绩归属人是否存在uidArr
        if (!is_array ($uidArr)){
            $uidArr = explode (',',$uidArr);
        }

        foreach ($data as $datum) {
            //计算业绩
            if (empty($datum['performance_uid'])){
                continue;
            }
            $datumPidArr = explode (',',$datum['performance_uid']);

            $datumPidCount = count($datumPidArr);
            foreach ($uidArr as $item) {
                if (!in_array ($item, $datumPidArr)){
                    continue;
                }

                //查看成交时间
//                $dealData = $dealModel->getOrderCompanyIdFindModel($datum['id'],$datum['certificate_code']);

//                if ($dealData){
//                    if (strtotime ($dealData['create_date']) >= strtotime ($startDate) && strtotime ($dealData['create_date']) <= strtotime ($endDate)){
                $count++;
                break;
//                    }
//                }
            }
        }
        return $count;
    }

    public function getBusinessIdCountModel($id)
    {
//        return $this->with ('businesss')->whereRaw('status=1 OR status=2')->select()->toArray ();
        return $this->whereRaw('status=1 OR status=2')->select()->toArray ();
    }

    public function getPhbModel( $startDate, $endDate,$idArr,$cerId)
    {
        //查询成交时间
        $certificateModel = new SystemCertificate();
        $orderDealChargebackModel = new OrderDealChargeback();

        $data = $this
            ->whereRaw('status = 1 OR status =2 OR status =3 OR status=4 OR status =5')
            ->whereBetweenTime ('sale_date',$startDate,$endDate)
            ->field('id,certificate_code,certificate_leixing,performance_uid,performance')
            ->select()->toArray();

        $result = [];

        $tdData = $orderDealChargebackModel->getBetDateTypeModel($startDate,$endDate);
        /**
         * 以每个人去统计
         */
        if (!is_array ($idArr)){
            $idArr = explode (',',$idArr);
        }

        foreach ($idArr as $id) {
            //统计退单
            $tdPrice = 0;
            $yj = 0;
            if ($tdData){
                foreach ($tdData as $tdDatum) {
                    //查询订单信息
                    $daOrder = $this->where (['id'=>$tdDatum['order_company_id'],'certificate_code'=>$tdDatum['certificate_code']])->find ();
                    //查询证书专业是否一致
                    $cid = $certificateModel->getWhereCertificateIdModel ($daOrder['certificate_leixing']);

                    if ($cid != $cerId || empty($cerId)){
                        continue;
                    }
                    if ($daOrder){
                        //查看业绩归属人有几人
                        $performanceArr = explode (',', $daOrder['performance_uid']);
                        foreach ($performanceArr as $performanceArrItem) {
                            if ($performanceArrItem == $id){
                                $tdPrice += $tdDatum['performance']/count($performanceArr);
                            }
                        }
                    }
                }
            }
            //统计订单
            foreach ($data as $idDatum) {
                //查询证书专业是否一致
                $cid = $certificateModel->getWhereCertificateIdModel ($idDatum['certificate_leixing']);

                if ($cid != $cerId || empty($cerId)){
                    continue;
                }
                $idArrs = explode (',',$idDatum['performance_uid']);
                $idCount = count($idArrs);
                foreach ($idArrs as $items) {
                    if ($items == $id){
                        $yj += $idDatum['performance']/$idCount;
                    }
                }
            }

            $result[] = [
                'performance_uid'=>$id,
                'all_out_price'=>$yj + $tdPrice
            ];
        }
        return $result;
    }

    /**
     * 财务补充
     * @param $certificate_code
     * @param $id
     * @return mixed
     */
    public function getExcelInfoModel($certificate_code,$id)
    {
        $data = $this->where (['certificate_code'=>$certificate_code,'id'=>$id])->field ('employ_unit_title as employ_unit,person_name,contract_price as get_price')->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }


    /**
     * Ⅰ类业绩统计 + 排行
     */
    public function getYileiWorkCount($startDate, $endDate,$cer,$uid)
    {

//        return  $this->withJoin ('priceUnit')
        return  $this->whereRaw('(status = 1 OR status =2  OR status=4 OR status =5) AND certificate_zhuanye ='.$cer.' AND get_uid in ('.$uid.')')
            ->whereBetweenTime ('create_date',$startDate,$endDate)
            ->field('sum(price_unit/people_num-contract_price) as all_out_price')
            ->select()->toArray();
    }

    /**
     * 获取证书业绩
     * @param $cer
     */
    public function getCertificateCode($idArr,$cer)
    {
        return $this->whereIn ('get_uid',$idArr)->where (['certificate_code'=>$cer])->field('contract_price,performance')->select()->toArray ();
    }

    /**
     * 完成订单操作
     * @param $id
     * @return \app\model\OrderEnterpriseCompany
     */
    public function completeOperateModel($id)
    {
        return $this->where (['id'=>$id])->update (['status'=>1]);
    }

    /**
     * 通过id 获取人才身份证
     * @param $id
     * @return mixed
     */
    public function getOrderPersonIdCardModel($id)
    {
        return $this->where (['id'=>$id])->value ('person_id_card');
    }

    /**
     * 获取应收款
     * @param $id
     * @param $cer
     * @return array|null
     */
    public function getYingshouModel($id,$cer): ?array
    {
//        $data = $this->with ('certificates')->where (['certificate_code'=>$cer,'id'=>$id])->findOrEmpty ();
        $data = $this->where (['certificate_code'=>$cer,'id'=>$id])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 业绩总表修改业绩
     * @param $param
     * @return \app\model\OrderEnterpriseCompany
     */
    public function editIdPerformanceModel($param)
    {
        return $this->where(['id'=>$param['id'],'certificate_code'=>$param['certificate_code']])->update (['performance'=>$param['performance'],'ad_performance'=>1]);
    }

    /**
     * 业绩总表添加备注
     * @param $param
     * @return \app\model\OrderEnterpriseCompany
     */
    public function editIdLabelModel($param)
    {
        return $this->where(['id'=>$param['id'],'certificate_code'=>$param['certificate_code']])->update (['label'=>$param['label']]);
    }

    /**
     * 查询聘用单位订单量
     * @param $title
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getPinYongUnitOrderNumModel($title): int
    {
        return $this->where (['employ_unit_title'=>$title])->count();
    }

    /**
     * 查询当前账号提交聘用单位订单量
     * @param $title
     * @param $uid
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getUidPinYongUnitOrderNumModel($title,$uid): int
    {
        return $this->where (['employ_unit_title'=>$title,'get_uid'=>$uid])->count();
    }

    /**
     * 查询订单编号相关订单数量
     * @param $cer
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getCerOrderNumModel($cer): int
    {
        return $this->where (['certificate_code'=>$cer])->count();
    }

    /**
     * 查询用户订单编号相关订单数量
     * @param $cer
     * @param $uid
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getUidCerOrderNumModel($cer,$uid): int
    {
        return $this->where (['certificate_code'=>$cer,'get_uid'=>$uid])->count();
    }

    /**
     * 通过聘用单位查询最新一条订单
     * @param $name
     * @return array|null
     */
    public function getEmployFind($name)
    {
        $data = $this->where (['employ_unit_title'=>$name])->order('create_date DESC')->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    /**
     * 通过身份证查询是否有关联订单
     * @param $idCard
     * @return array|false
     */
    public function getIdCardOrderInfoModel($idCard)
    {
        $data = $this->where (['person_id_card'=>$idCard])->order('create_date DESC')->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return false;
    }

    /**
     * 完成订单收付款
     * @param $id
     * @return
     */
    public function completeOrderSFKModel($id)
    {
        return $this->where(['id' => $id])->update(['sfk_status' => 2,'sfk_wc_date'=>date('Y-m-d H:i:s')]);
    }

    /**
     * 完成后点击返回按钮 状态改为未完成 完成时间改为空
     * @param $id
     * @return OrderEnterpriseCompany
     */
    public function backCompleteOrderSFKModel($id)
    {
        return $this->where(['id' => $id])->update(['sfk_status' => 1, 'sfk_wc_date' => null]);
    }

    /**
     * 通过id 证书编号查询成交该订单的人才
     * @param $id
     * @param $cer
     * @return mixed
     */
    public function getCerIdcardFindModel($id,$cer)
    {
        return $this->where (['id'=>$id,'certificate_code'=>$cer])->value ('person_id_card');
    }


    /**
     * 查询审核人为当前id的信息id集
     * @param $id
     * @return array
     */
    public function getResponsibilityUidDataModel($id)
    {
        return $this->where (['responsibility_uid'=>$id])->column ('id');
    }
    public function editResponsibilityUidDataModel($idArr,$id)
    {
        return $this->whereIn ('id',$idArr)->update (['responsibility_uid'=>$id]);
    }

    /**
     * 查询收证人员为当前id的信息id集
     * @param $id
     * @return array
     */
    public function getGetUidDataModel($id)
    {
        return $this->where (['get_uid'=>$id])->column ('id');
    }
    public function editGetUidDataModel($idArr,$id,$org)
    {
        return $this->whereIn ('id',$idArr)->update (['get_uid'=>$id,'get_org'=>$org]);
    }

    /**
     * 查询发起人为当前id的信息id集
     * @param $id
     * @return array
     */
    public function getSubmitUidDataModel($id)
    {
        return $this->where (['submit_id'=>$id])->column ('id');
    }
    public function editSubmitUidDataModel($idArr,$id)
    {
        return $this->whereIn ('id',$idArr)->update (['submit_id'=>$id]);
    }

    /**
     * 证书注册完成
     * @param $id
     * @return \app\model\OrderEnterpriseCompany
     */
    public function certificateRegModel($id)
    {
        return $this->where(['id' => $id])->update(['status' => 2]);
    }

    public function getIdStatusModel($id)
    {
        return $this->where (['id'=>$id])->value ('status');
    }

    public function getWhereIdOrderByCreateData($id)
    {
//        $data = $this->with ('certificates')->where(['id' => $id])->order('create_date DESC')->findOrEmpty();
        $data = $this->where(['id' => $id])->order('create_date DESC')->findOrEmpty();
        if (!$data->isEmpty()) {
            return $data->toArray();
        }
        return null;
    }

    public function getWhereIdCardOrderByCreateData($idCard)
    {
//        $data = $this->with ('certificates')->where(['person_id_card' => $idCard,'status'=>5])->order('create_date ASC')->findOrEmpty();
        $data = $this->where(['person_id_card' => $idCard,'status'=>5])->order('create_date ASC')->findOrEmpty();
        if (!$data->isEmpty()) {
            return $data->toArray();
        }
        return null;
    }
    public function getWhereIdCardFindData($idCard)
    {
//        $data = $this->with ('certificates')->where(['person_id_card' => $idCard])->order('create_date ASC')->findOrEmpty();
        $data = $this->where(['person_id_card' => $idCard])->order('create_date ASC')->findOrEmpty();
        if (!$data->isEmpty()) {
            return $data->toArray();
        }
        return null;
    }

    /**
     * 业绩统计证书注册
     * @param $uid
     * @param $status
     * @param $startDate
     * @param $endDate
     * @return int[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
//    public function getUidCertificateCodeModel($uid,$status,$startDate,$endDate)
//    {
//
//        $orderChargebackModel = new OrderDealChargeback();
//
////        $data = $this->with ('certificates')->select ()->toArray ();
////        $data = $this->field('id,sale_uid,get_uid,certificate_code,performance_uid,performance')->select ()->toArray ();
//
////        $data = $this->whereBetweenTime ('sale_date',$startDate,$endDate)->column ('certificate_code,id,performance_uid,performance');
//        //所有已售出的成交记录
//        $orderDealModel = new OrderDeal();
//        $data = $orderDealModel->getDateCerIdDataModel($startDate,$endDate,$status);
//        /**
//         * 同步一下公司证书销售时间与成交时间 一次性脚本 2023-12-28
//         */
////        $comData = $this->whereNotNull ('sale_date')->column ('sale_date,id,certificate_code');
////
////        if (!empty($comData)){
////            foreach ($comData as $comDatum) {
////                $orD = $orderDealModel->getOrderCompanyIdFindModel($comDatum['id'],$comDatum['certificate_code']);
////
////                if ($orD){
////                    if(strtotime ($comDatum['sale_date']) != strtotime($orD['create_date'])){
////                        $orderDealModel->editIdCerDataModel ($comDatum['id'],$comDatum['certificate_code'],['create_date'=>$comDatum['sale_date']]);
////                    }
////                }
////            }
////        }
//
//        $container = [
//            'order_num'=>0,
//            'order_price'=>0
//        ];
//        if ($data) {
//            foreach ($data as $item) {
//                //查询订单成交状态是新出 续出 续签
////                if (!$orderDealModel->getCerStatusEmptyModel($item,$status)){
////                    continue;
////                }
//                $findComData = $this->where(['certificate_code'=>$item])
//                    ->field('id,certificate_code,performance_uid,performance')
//                    ->find ();
//
//                if (!$findComData){
//                    continue;
//                }
//
//                //满足条件 计算业绩、单量
//                $dUidArr = explode (',',$findComData['performance_uid']);
//                if (in_array ($uid,$dUidArr)){
//                    $container['order_num']++;
//                }
//
//                foreach ($dUidArr as $id) {
//                    if ($uid == $id){
//                        $container['order_price'] += $findComData['performance']/count ($dUidArr);
//                    }
//                }
//
//                //查询退单信息
//                $backDataPerformance = $orderChargebackModel->getOrderBackPerformance($findComData['id'],$findComData['certificate_code'],$startDate,$endDate);
//                if (!empty($backDataPerformance)){
//                    //今天成交订单  今日订单+1 业绩+500
//                    //第二天退单 第一天不变 第二日订单-1 业绩-500
//                    if (in_array ($uid,$dUidArr)){
//                        $container['order_num']--;
//                    }
//                    foreach ($dUidArr as $id) {
//                        if ($uid == $id){
//                            $container['order_price'] += $backDataPerformance/count ($dUidArr);
//                        }
//                    }
//                }
//            }
//        }
//
//        return $container;
//    }
    public function getUidCertificateCodeModel($uid,$status,$startDate,$endDate)
    {

        $orderChargebackModel = new OrderDealChargeback();
        //所有已售出的成交记录
        $orderDealModel = new OrderDeal();
//        $data = $orderDealModel->getDateCerIdDataModel($startDate,$endDate);

        /**
         * 同步一下公司证书销售时间与成交时间 一次性脚本 2023-12-28
         */
//        $comData = $this->whereNotNull ('sale_date')->column ('sale_date,id,certificate_code');
//
//        if (!empty($comData)){
//            foreach ($comData as $comDatum) {
//                $orD = $orderDealModel->getOrderCompanyIdFindModel($comDatum['id'],$comDatum['certificate_code']);
//
//                if ($orD){
//                    if(strtotime ($comDatum['sale_date']) != strtotime($orD['create_date'])){
//                        $orderDealModel->editIdCerDataModel ($comDatum['id'],$comDatum['certificate_code'],['create_date'=>$comDatum['sale_date']]);
//                    }
//                }
//            }
//        }

        $container = [
            'new_order_num'=>0, //新出
            'new_order_price'=>0,
            'xq_order_num'=>0, //续签
            'xq_order_price'=>0,
            'xc_order_num'=>0, //续出
            'xc_order_price'=>0
        ];

        $data = Db::table('order_enterprise_company')->alias ('c')
            ->join ('order_deal d','c.certificate_code=d.certificate_code')
            ->whereBetweenTime ('d.create_date',$startDate,$endDate)
            ->field('d.status,c.id,c.certificate_code,c.performance,c.performance_uid')->select();

        foreach ($data as $item){
//            满足条件 计算业绩、单量
            $dUidArr = explode (',',$item['performance_uid']);
            if (in_array ($uid,$dUidArr)){
                if($item['status'] == 1){ //新出
                    $container['new_order_num']++;
                }else if($item['status'] == 2){
                    $container['xq_order_num']++;
                }else if($item['status'] == 3){
                    $container['xc_order_num']++;
                }
            }

            foreach ($dUidArr as $id) {
                if ($uid == $id){
                    if($item['status'] == 1){ //新出
                        $container['new_order_price'] += $item['performance']/count ($dUidArr);
                    }else if($item['status'] == 2){
                        $container['xq_order_price'] += $item['performance']/count ($dUidArr);
                    }else if($item['status'] == 3){
                        $container['xc_order_price'] += $item['performance']/count ($dUidArr);
                    }
                }
            }

//                查询退单信息
            $backDataPerformance = $orderChargebackModel->getOrderBackPerformance($item['id'],$item['certificate_code'],$startDate,$endDate);
            if (!empty($backDataPerformance)){
                //今天成交订单  今日订单+1 业绩+500
                //第二天退单 第一天不变 第二日订单-1 业绩-500
                if (in_array ($uid,$dUidArr)){

                    if($item['status'] == 1){ //新出
                        $container['new_order_num']--;
                    }else if($item['status'] == 2){
                        $container['xq_order_num']--;
                    }else if($item['status'] == 3){
                        $container['xc_order_num']--;
                    }
                }
                foreach ($dUidArr as $id) {
                    if ($uid == $id){
                        if($item['status'] == 1){ //新出
                            $container['new_order_price'] += $backDataPerformance/count ($dUidArr);
                        }else if($item['status'] == 2){
                            $container['xq_order_price'] += $backDataPerformance/count ($dUidArr);
                        }else if($item['status'] == 3){
                            $container['xc_order_price'] += $backDataPerformance/count ($dUidArr);
                        }
                    }
                }
            }
        }
//        if ($data) {
//            foreach ($data as $item) {
//                //查询订单成交状态是新出 续出 续签
////                if (!$orderDealModel->getCerStatusEmptyModel($item,$status)){
////                    continue;
////                }
//                $findComData = $this->where(['certificate_code'=>$item['certificate_code']])
//                    ->field('id,certificate_code,performance_uid,performance')
//                    ->find ();
////
//                if (!$findComData){
//                    continue;
//                }
//
////                满足条件 计算业绩、单量
//                $dUidArr = explode (',',$findComData['performance_uid']);
//                if (in_array ($uid,$dUidArr)){
//                    if($item['status'] == 1){ //新出
//                        $container['new_order_num']++;
//                    }else if($item['status'] == 2){
//                        $container['xq_order_num']++;
//                    }else if($item['status'] == 3){
//                        $container['xc_order_num']++;
//                    }
//                }
//
//                foreach ($dUidArr as $id) {
//                    if ($uid == $id){
//                        if($item['status'] == 1){ //新出
//                            $container['new_order_price'] += $findComData['performance']/count ($dUidArr);
//                        }else if($item['status'] == 2){
//                            $container['xq_order_price'] += $findComData['performance']/count ($dUidArr);
//                        }else if($item['status'] == 3){
//                            $container['xc_order_price'] += $findComData['performance']/count ($dUidArr);
//                        }
//                    }
//                }
//
////                查询退单信息
//                $backDataPerformance = $orderChargebackModel->getOrderBackPerformance($findComData['id'],$findComData['certificate_code'],$startDate,$endDate);
//                if (!empty($backDataPerformance)){
//                    //今天成交订单  今日订单+1 业绩+500
//                    //第二天退单 第一天不变 第二日订单-1 业绩-500
//                    if (in_array ($uid,$dUidArr)){
//
//                        if($item['status'] == 1){ //新出
//                            $container['new_order_num']--;
//                        }else if($item['status'] == 2){
//                            $container['xq_order_num']--;
//                        }else if($item['status'] == 3){
//                            $container['xc_order_num']--;
//                        }
//                    }
//                    foreach ($dUidArr as $id) {
//                        if ($uid == $id){
//                            if($item['status'] == 1){ //新出
//                                $container['new_order_price'] += $backDataPerformance/count ($dUidArr);
//                            }else if($item['status'] == 2){
//                                $container['xq_order_price'] += $backDataPerformance/count ($dUidArr);
//                            }else if($item['status'] == 3){
//                                $container['xc_order_price'] += $backDataPerformance/count ($dUidArr);
//                            }
//                        }
//                    }
//                }
//            }
//        }
        return $container;
    }
    /**
     * 业绩统计公司证书统计优化
     */
    public function getUidCertificateCodeYhModel($uid,$cer,$startDate,$endDate)
    {

        /**
         * 同步一下公司证书销售时间与成交时间 一次性脚本 2023-12-28
         */
//        $comData = $this->whereNotNull ('sale_date')->column ('sale_date,id,certificate_code');
//
//        if (!empty($comData)){
//            foreach ($comData as $comDatum) {
//                $orD = $orderDealModel->getOrderCompanyIdFindModel($comDatum['id'],$comDatum['certificate_code']);
//
//                if ($orD){
//                    if(strtotime ($comDatum['sale_date']) != strtotime($orD['create_date'])){
//                        $orderDealModel->editIdCerDataModel ($comDatum['id'],$comDatum['certificate_code'],['create_date'=>$comDatum['sale_date']]);
//                    }
//                }
//            }
//        }

        $orderChargebackModel = new OrderDealChargeback();
        $container = [
            'order_num'=>0,
            'order_price'=>0
        ];

        $findComData = $this->where(['certificate_code'=>$cer])
            ->field('id,certificate_code,performance_uid,performance')
            ->find ();

        //满足条件 计算业绩、单量
        $dUidArr = explode (',',$findComData['performance_uid']);
        if (in_array ($uid,$dUidArr)){
            $container['order_num']++;
        }

        foreach ($dUidArr as $id) {
            if ($uid == $id){
                $container['order_price'] += $findComData['performance']/count ($dUidArr);
            }
        }

        //查询退单信息
        $backDataPerformance = $orderChargebackModel->getOrderBackPerformance($findComData['id'],$findComData['certificate_code'],$startDate,$endDate);
        if (!empty($backDataPerformance)){
            //今天成交订单  今日订单+1 业绩+500
            //第二天退单 第一天不变 第二日订单-1 业绩-500
            if (in_array ($uid,$dUidArr)){
                $container['order_num']--;
            }
            foreach ($dUidArr as $id) {
                if ($uid == $id){
                    $container['order_price'] += $backDataPerformance/count ($dUidArr);
                }
            }
        }

        return $container;
    }

    /**
     * 报表中心-证书注册占比统计
     */
    public function getWhereModel($where,$uidArr,$startDate,$endDate)
    {

        $orderRegModel = new OrderEnterpriseCertificateRegister();
        $orderChargebackModel = new OrderDealChargeback();
        $orderDealModel = new OrderDeal();

        //查询需求表证书编号
        $where .= ' and get_uid IN ('.implode ($uidArr,',').')';
//        $cerArr = $orderRegModel->getWhereModel ($where);

        //根据证书编号查询补充信息
        $data = $this->whereBetweenTime ('sale_date',$startDate,$endDate)->whereRaw ($where)->whereIn ('get_uid',$uidArr)->select ()->toArray ();

        //判断成交时间是否满足
        $container = [
            'order_num'=>0,
            'order_price'=>0
        ];
        foreach ($data as $datum) {
//            $dealData = $orderDealModel->getCerComIdDateDataModel ($datum['id'],$datum['certificate_code'],'',$startDate,$endDate);
//            if ($dealData){
//                if (strtotime ($dealData['create_date']) >= strtotime($startDate) && strtotime ($dealData['create_date']) <= strtotime ($endDate)){
            //满足条件 计算业绩、单量
            $dUidArr = explode (',',$datum['performance_uid']);
            foreach ($dUidArr as $item) {
                if (in_array ($item,$uidArr)){
                    $container['order_num']++;
                    break;
                }
            }

            foreach ($dUidArr as $item) {
                if (in_array ($item,$uidArr)){
                    $container['order_price'] += $datum['performance']/count ($dUidArr);
                }
            }

            //查询退单信息
            $backData = $orderChargebackModel->getCidCerTypeDataModel($datum['id'],$datum['certificate_code'],$startDate,$endDate);
            if (!empty($backData)){
                //今天成交订单  今日订单+1 业绩+500
                //第二天退单 第一天不变 第二日订单-1 业绩-500
                //满足条件 计算业绩、单量
                foreach ($dUidArr as $items) {
                    if (in_array ($items,$uidArr)){
                        $container['order_num']--;
                        break;
                    }
                }
                foreach ($dUidArr as $items) {
                    if (in_array ($items,$uidArr)){
                        $container['order_price'] += $backData['performance']/count ($dUidArr);
                    }
                }
//                    }
//                }
            }
        }

        return $container;

    }

    /**
     * 报表中心-提成比例
     */
    public function getEnterPriseDataModel($bid)
    {
//        return $this->with ('certificates')->whereIn ('enter_certificate_code',$enterCer)
//        return $this->whereIn ('enter_certificate_code',$enterCer)

        return $this->where ('business_id',$bid)
//            ->fetchSql(true)
//            ->whereRaw ('status = 1 OR status = 2 OR status = 3 OR status = 4 OR status =5')
            ->whereIn('status',[1,2,3,4,5])
//            ->field('id,performance_uid,performance,certificate_code')
//            ->select ()->toArray ();
            ->column ('id,performance_uid,performance,certificate_code');
    }

    /**
     * 查询今日订单个数
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getToDayCountFindModel():int
    {
        return  $this->whereDay('create_date')->count ();
    }

}